﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using PBOffice.DAL;

public partial class SaveMySchedule : System.Web.UI.Page
{
    MyScheduleBLL sch = new MyScheduleBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            if (Request.QueryString["day"] != "")
            {
                UpdateSchedule();
            }
            MyScheduleBLL type = new MyScheduleBLL();
            ddlMeetingType.DataSource = type.GetMeetingType();
            ddlMeetingType.DataTextField = "MeetingTypeName";
            ddlMeetingType.DataValueField = "MeetingTypeNo";
            ddlMeetingType.DataBind();
        }
    }
    #region 保存退出
    /// <summary>
    /// 保存退出按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Schedule schedule = new Schedule();
        schedule.SchTopic = txtTitle1.Text.Trim(); //主题
        schedule.SchAddr = txtAddress.Text.Trim();  //地址
        schedule.SchBeginTime = Convert.ToDateTime(txtBeginTime.Text.Trim());//开始时间
        schedule.SchEndTime = Convert.ToDateTime( txtEndTime.Text.Trim());  //结束时间
        schedule.SchContent = txtContent.Text.Trim();//内容
        schedule.SchCreateTime = Convert.ToDateTime(lblTime.Text);
        User user = new PBOffice.DAL.User();
        schedule.User = user;
        schedule.User.UserId = lblUser.Text;
        MeetingType mt = new MeetingType();
        schedule.MeetingType = mt;
        schedule.MeetingType.MeetingTypeNo = Convert.ToInt32(ddlMeetingType.SelectedValue);//会议类型
        if (cbOpen.Checked)      //是否公开
            schedule.SchIsPublish = true;
        else
            schedule.SchIsPublish = false;
        string strday = Request.QueryString["day"];   //获取指定日
        DateTime schDate = Convert.ToDateTime(strday);
        bool result; 
            
        if (Request.QueryString["day"]=="")
        {
            result= sch.AddSchedule(schedule, schDate); //添加日程
        }
        else
        {
            DateTime dt=Convert.ToDateTime(Request.QueryString["day"]);
            result = sch.UpdateSchedule(schedule,dt);
        }

        if (result == true)
        {
            Response.Redirect("ScheduleManager.aspx");//如果保存成功跳转到个人日程管理页面
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "key", "<script>alert('保存失败！')</script>");
        }
        

    } 
    #endregion

    #region 删除预约人
    /// <summary>
    /// 删除指定预约人
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOne_Click(object sender, EventArgs e)
    {
        string person = libUser.SelectedItem.Value;//获取指定的预约人
        int userNo = Convert.ToInt32(person);
        sch.DeletePreContract(userNo);
    } 
    #endregion

    #region 退出按钮
    /// <summary>
    /// 退出按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnQuit_Click(object sender, EventArgs e)
    {
        this.btnQuit.Attributes.Add("onclick", "window.close()");
    } 
    #endregion

    #region 预约他人
    /// <summary>
    /// 预约他人按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibOthers_Click(object sender, ImageClickEventArgs e)
    {
        Panel.Visible = true;
        var organs = new OrganManager();
        foreach (var organ in organs.ShowAllOrganInfo())
        {
            TreeNode root = new TreeNode(organ.OrganShortName ?? organ.OrganId, organ.OrganNo.ToString());
            var depart = new DepartmentManager();
            var dep = depart.ShowDepInfoByOrganNo(organ.OrganNo);
            foreach (var item in dep)
            {
                TreeNode subTN = new TreeNode(item.DepDescribe ?? item.DepId, item.DepNo.ToString());
                addSubDeparts(subTN, item.DepNo);
            }
            tvDep.Nodes.Add(root);
        }
    } 
    #endregion

     private void addSubDeparts(TreeNode tn,int depNo)
      {
            var depart =new DepartmentManager();
            var subDeparts=depart.GetSubDepartments(depNo);
            foreach (var subDepart in subDeparts)
            {
                TreeNode subTN=new TreeNode (subDepart.DepDescribe ?? subDepart.DepId,subDepart.DepNo.ToString());
                addSubDeparts(subTN,subDepart.DepNo);
                tn.ChildNodes.Add(subTN);
            }
        }

     #region 修改日程
     /// <summary>
     /// 修改日程
     /// </summary>
     protected void UpdateSchedule()
     {
         DateTime dt = Convert.ToDateTime(Request.QueryString["day"]);
         User user = (User)Session["userManage"];
         List<v_DepUserSch> schedule = new MyScheduleBLL().ShowSchedule(user.UserNo, dt);
         foreach (var item in schedule)
         {
             txtTitle1.Text = item.SchTopic;
             txtAddress.Text = item.SchAddr;
             txtBeginTime.Text = item.SchBeginTime.ToString();
             txtEndTime.Text = item.SchEndTime.ToString();
             txtContent.Text = item.SchContent;
             lblUser.Text = item.UserId;
             lblTime.Text = item.SchCreateTime.ToString();
             MeetingType mt = new MeetingType();
             mt.MeetingTypeNo = item.MeetingTypeNo;
             ddlMeetingType.SelectedValue = mt.MeetingTypeNo.ToString();
             if (item.SchIsPublish == true)
             {
                 cbOpen.Checked = true;
             }
             else
             {
                 cbOpen.Checked = false;
             }
         }
     } 
     #endregion


}
  